package com.simple.elpmobile.onlinecase.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.Toast;

import com.simple.elpmobile.onlinecase.R;
import com.simple.elpmobile.onlinecase.engine.AppEngine;
import com.simple.elpmobile.onlinecase.engine.IManager;
import com.simple.elpmobile.onlinecase.engine.manager.NetworkManager;
import com.simple.elpmobile.onlinecase.engine.network.ResponseHandler;
import com.simple.elpmobile.onlinecase.ui.base.BaseActivity;
import com.simple.elpmobile.onlinecase.ui.base.CustomToast;
import com.simple.elpmobile.onlinecase.ui.base.LoadingDialog;

public class RegistActivity extends BaseActivity implements OnClickListener {
	private EditText nickNameTxt, passTxt, telTxt, ageTxt,realNameTxt;
	private Spinner sexSpinner;
	private Button registBtn;
	private LinearLayout backLayout;

	private LoadingDialog loadingDialog;

	public final static int SHOW_LOGIN = 0;
	public final static int TOKEN_EXPIRED = 1;
	private int showType = SHOW_LOGIN;
	public final static String SHOW_TYPE = "showType";
	private String sexStr = "男", nick, tel, pass, age,name;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_regist);
		showType = getIntent().getIntExtra(SHOW_TYPE, SHOW_LOGIN);
		initView();
	}

	private void initView() {
		nickNameTxt = (EditText) findViewById(R.id.name_eidt);
		passTxt = (EditText) findViewById(R.id.pass_eidt);
		telTxt = (EditText) findViewById(R.id.tel_eidt);
		ageTxt = (EditText) findViewById(R.id.age_eidt);
		realNameTxt = (EditText) findViewById(R.id.real_name_eidt);
		sexSpinner = (Spinner) findViewById(R.id.sex_sp);
		registBtn = (Button) findViewById(R.id.regist_btn);
		backLayout = (LinearLayout) findViewById(R.id.imageViewGoBack);
		registBtn.setOnClickListener(this);
		backLayout.setOnClickListener(this);
		List<String> sex = new ArrayList<String>();
		sex.add("男");
		sex.add("女");
		ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
				R.layout.sex_item, R.id.sexitem, sex);
		sexSpinner.setAdapter(mAdapter);
		sexSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if (arg2 == 0) {
					sexStr = "男";
				} else if (arg2 == 1) {
					sexStr = "女";
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {

			}
		});
	}

	public boolean inputSucess() {
		nick = nickNameTxt.getText().toString().trim();
		pass = passTxt.getText().toString().trim();
		tel = telTxt.getText().toString().trim();
		age = ageTxt.getText().toString().trim();
		name= realNameTxt.getText().toString().trim();
		Pattern pattern = Pattern.compile(
				"[0-9a-zA-Z]{1,23}",
				Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(nick);
		
		if (!matcher.matches()) {
			CustomToast.showToast(this, "用户名输入有误，用户名只能包含字母！", 200);
			return false;
		}
		
		if (TextUtils.isEmpty(name)) {
			CustomToast.showToast(this, "真实姓名不能为空！", 200);
			return false;
		}
		
		if (TextUtils.isEmpty(pass)) {
			CustomToast.showToast(this, "密码不能为空！", 200);
			return false;
		}

		if (TextUtils.isEmpty(tel)) {
			CustomToast.showToast(this, "电话不能为空！", 200);
			return false;
		}

		if (TextUtils.isEmpty(age)) {
			CustomToast.showToast(this, "年龄不能为空！", 200);
			return false;
		}
		return true;

	}

	private void execRegist() {
		if (inputSucess()) {
			showLoading("注册中...");
			((NetworkManager) (AppEngine.getInstance()
					.getManager(IManager.NETWOTK_ID))).regist(name, pass, tel,
					age, sexStr,nick, new ResponseHandler.CommitHandler() {

						@Override
						public void OnSucess(String info) {
							CustomToast.showToast(getApplicationContext(),
									info, 1000);
							Intent intent = new Intent(RegistActivity.this,
									LoginActivity.class);
							startActivity(intent);
							finish();
						}

						@Override
						public void OnFail(int code, String str) {
							dismissLoading();
							CustomToast.showToast(getApplicationContext(), str,
									1000);
						}
					});
		}
	}

	private final String DIALOG_LOCKER = "dialogLocker";

	private void showLoading(String msg) {
		synchronized (DIALOG_LOCKER) {
			if (loadingDialog != null) {
				loadingDialog.dismissDialog();
				loadingDialog = null;
			}
			loadingDialog = new LoadingDialog(this);
			loadingDialog.showDialog(msg);
		}
	}

	private void dismissLoading() {
		synchronized (DIALOG_LOCKER) {
			if (loadingDialog != null) {
				loadingDialog.dismissDialog();
				loadingDialog = null;
			}
		}
	}

	@Override
	public void onBackPressed() {

	}

	@Override
	public byte activityId() {
		return 0;
	}

	public void onResume() {
		super.onResume();

	}

	public void onPause() {
		super.onPause();

	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.imageViewGoBack:
			finish();
			break;

		case R.id.regist_btn:
			execRegist();
			break;

		default:
			break;
		}

	}

}